from flask import Flask as _Flask,jsonify
import time
from flask import render_template
from flask.json import JSONEncoder as _JSONEncoder
import decimal
from real_time import utils
import string
import redis

ip = '127.0.0.1'
password = ''
r = redis.Redis(host=ip, password=password, port=6379, db=0, decode_responses=True)

class JSONEncoder(_JSONEncoder):
        def default(self, o):
            if isinstance(o, decimal.Decimal):
                return float(o)
            super(_JSONEncoder, self).default(o)

class Flask(_Flask):
    json_encoder = JSONEncoder


app = Flask(__name__)


@app.route('/hello')
def hello_world():
    return 'Hello World!'

#@app.route('/login')
# def hello_world2():
#     name = request.values.get("name")
#     pwd = request.values.get("pwd")
#     return f'name={name}.pwd={pwd}'

# @app.route('/abc')
# def hello_world1():
#     id = request.values.get("id")
#     return f"""
#     <form action="/login">
#             账号：<input name="name"><br>
#             密码：<input name="pwd">
#             <input type="submit">
#         </form>
#     """

@app.route('/')
def hello_word3():
    return render_template("main.html")

@app.route('/time')
def get_time():
    return utils.get_time()

@app.route('/c1')
def get_c1_data():
    customer=r.get("customer_num")
    print(customer)
    totalmoney=r.get("total_money")
    # print(totalmoney)
    # print(type(totalmoney))
    total_money=round(float(totalmoney),2)
    ordernum=r.get("order_num")
    print(ordernum)
    data=[]
    data.append(customer)
    data.append(total_money)
    data.append(ordernum)
    return jsonify({"customer":int(data[0]),"total_money":data[1],"ordernum":int(data[2])})

@app.route('/c2')
def get_c2_data():
    listc=[]
    for a,b in r.zrevrange(name="area_num", start=0, end=-1, withscores=True):
        if a=='黑龙':
            a='黑龙江'
        if b=='内蒙':
            b='内蒙古'
        listc.append({"name":a,"value":int(b)})
    return jsonify({"areanum":listc})


@app.route('/l1')
def get_l1_data():
    top10_book1 = r.zrevrange(name="top10_book", start=0, end=9)
    listb= []
    for i in range(0, 10):
        top10_book = r.zscore(name="top10_book", value=top10_book1[i])
        #print(top10_book)
        listb.append(int(top10_book))
    return jsonify({"book": top10_book1, "num": listb})
@app.route('/l2')
def get_l2_data():
    listd = []
    for c, d in r.zrevrange(name="payment", start=0, end=-1, withscores=True):
        listd.append({"name": c, "value": int(d)})
    return jsonify({"payment": listd})


@app.route('/r1')
def get_r1_data():
    top10_press1 = r.zrevrange(name="top10_press", start=0, end=9)
    lista = []
    for i in range(0, 10):
        top10_press = r.zscore(name="top10_press", value=top10_press1[i])
        #print(top10_press)
        lista.append(int(top10_press))
    return jsonify({"city": top10_press1,"confirm": lista})
# @app.route('/r2')
# def get_r2_data():
#     while(true):
#         x=np.random.randint(100, 120)
#         time.sleep(1)
#         return jsonify({"value":x})
if __name__ == '__main__':
    app.run(host="0.0.0.0",port=5000)
